package com.gpframework.module.manufacture.service.impl;

import cn.hutool.core.convert.Convert;
import cn.hutool.core.lang.TypeReference;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.gpframework.common.enums.YesNoEnum;
import com.gpframework.common.exception.BaseException;
import com.gpframework.common.utils.DateUtils;
import com.gpframework.common.utils.StringUtils;
import com.gpframework.module.manufacture.domain.*;
import com.gpframework.module.manufacture.domain.req.GpProductionDetailsUpdate;
import com.gpframework.module.manufacture.domain.vo.GpProductionDetailsVo;
import com.gpframework.module.manufacture.mapper.GpProductionDetailsMapper;
import com.gpframework.module.manufacture.service.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
/**
 * @ClassName GpProductionDetails
 * @Author gpframework
 * @Date 2023-12-11
 * @Version 1.0.0
 * @Description 做货明细Service业务层处理
 */
@Service
public class GpProductionDetailsServiceImpl extends ServiceImpl<GpProductionDetailsMapper, GpProductionDetails> implements IGpProductionDetailsService {

    @Autowired
    private IGpDyeingFactoryService iGpDyeingFactoryService;
    @Autowired
    private IGpGreyClothService iGpGreyClothService;
    @Autowired
    private IGpProduceProductService iGpProduceProductService;
    @Autowired
    private IGpColourComparisonService iGpColourComparisonService;
    @Autowired
    private IGpColourRemarksService iGpColourRemarksService;
    @Autowired
    private IGpMakingGoodsRequirementDetailsService iGpMakingGoodsRequirementDetailsService;
    @Autowired
    private IGpCodingSpecificationsService iGpCodingSpecificationsService;
    @Autowired
    private IGpDeliveryLocationService iGpDeliveryLocationService;
    @Autowired
    private IGpLogisticsService iGpLogisticsService;
    @Autowired
    private IGpMakingGoodsRemarksService iGpMakingGoodsRemarksService;
    @Autowired
    private  IGpRemarksService iGpRemarksService;
    @Autowired
    private  IGpProductionSchedulingService iGpProductionSchedulingService;

    @Autowired
    private IGpProductionApplicationService iGpProductionApplicationService;

    /**
     * @Description 查询列表返回VO 用于返回给前端的列表接口
     * @Author gpframework
     * @Date 2023-12-11
     * @Param [lqw]
     * @Return List<GpProductionDetailsVo>
     */
    @Override
    public List<GpProductionDetailsVo> listVo(QueryWrapper<GpProductionDetails> lqw) {
        return this.baseMapper.listVo(lqw);
    }

    /**
     * @Description 通过查询详情VO 用于返回给前端的列详情接口
     * @Author gpframework
     * @Date 2023-12-11
     * @Param [id]
     * @Return GpProductionDetailsVo
     */
    @Override
    public GpProductionDetailsVo getVoById(Long id) {
        return this.baseMapper.getVo(id);
    }

    @Override
    public boolean edit(List<GpProductionDetailsUpdate> list) {
        List<GpProductionDetails> updateList = new ArrayList<>();
        for (GpProductionDetailsUpdate gpProductionDetailsUpdate : list) {


            GpProductionDetails gpProductionDetails = Convert.convert(new TypeReference<GpProductionDetails>() {}, gpProductionDetailsUpdate);
            GpProductionScheduling gpProductionScheduling = iGpProductionSchedulingService.getOne(new QueryWrapper<GpProductionScheduling>().eq("production_details_id", gpProductionDetails.getId()));
            if (gpProductionScheduling!=null && gpProductionDetailsUpdate.getDyeVatNo()!=null){
                gpProductionScheduling.setDyeVatNo(gpProductionDetailsUpdate.getDyeVatNo());
                iGpProductionSchedulingService.updateById(gpProductionScheduling);
            }
            this.init(gpProductionDetails);

            gpProductionDetails.setLeaveGreyNum(gpProductionDetailsUpdate.getStockGreyNum());


            updateList.add(gpProductionDetails);
        }
        return this.updateBatchById(updateList);
    }

    @Override
    public boolean editOne(GpProductionDetailsUpdate gpProductionDetailsUpdate) {
        //GpProductionDetails gpProductionDetails = Convert.convert(new TypeReference<GpProductionDetails>() {}, gpProductionDetailsUpdate);
        //this.init(gpProductionDetails);
        //return this.updateById(gpProductionDetails);
        return false;
    }

    /**
     * 复制
     * @param id
     * @param copyNum
     * @return
     */
    @Override
    public boolean copy(Long id, Integer copyNum) {
        GpProductionDetails gpProductionDetails = this.getById(id);
        if (gpProductionDetails == null) {
            return false;
        }
        for (int i = 0; i < copyNum; i++) {
            gpProductionDetails.setId(null);
            // 生成订单号
            GpDyeingFactory dyeingFactory = iGpDyeingFactoryService.getById(gpProductionDetails.getDyeingFactoryId());
            if (dyeingFactory != null) {
                gpProductionDetails.setSn(this.getSn(dyeingFactory.getCode()));
            } else {
                gpProductionDetails.setSn(null);
            }
            gpProductionDetails.setLeaveGreyNum(gpProductionDetails.getStockGreyNum());
            this.save(gpProductionDetails);
        }
        return true;
    }

    @Override
    public boolean splitOrders(Long id, Integer splitNum) {
        GpProductionDetails gpProductionDetails = this.getById(id);

        if (gpProductionDetails == null) {
            return false;
        }
        //改变原有订单
        if (gpProductionDetails.getStockGreyNum()==null){
            throw new BaseException("没有胚布库存剩余数量");
        }
        //改变原有订单
        if (gpProductionDetails.getLeaveGreyNum()-splitNum<0){
            throw new BaseException("拆分的胚布库存剩余数量:"+splitNum+"大于原订单数:"+gpProductionDetails.getStockGreyNum());
        }

        gpProductionDetails.setLeaveGreyNum(gpProductionDetails.getLeaveGreyNum()-splitNum);
        this.updateById(gpProductionDetails);


        //String sn = gpProductionDetails.getSn();
        //String newSn = "";
        //String[] split = sn.split("-");
        //if (split.length < 5){
        //    newSn = split[0] + "-" + split[1] + "-" + split[2] + "-" +  split[3] +"-"+1;
        //} else {
        //    newSn = split[0] + "-" + split[1] + "-" + split[2] + "-" + split[3] + "-" + (Integer.parseInt(split[4])+ 1);
        //}
        //新增拆分订单
        GpProductionDetails addProductionDetails = Convert.convert(new TypeReference<GpProductionDetails>() {
        }, gpProductionDetails);
        addProductionDetails.setId(null);
        addProductionDetails.setStockGreyNum(splitNum);
        addProductionDetails.setLeaveGreyNum(splitNum);
        addProductionDetails.setSn(null);
        this.save(addProductionDetails);
        return true;

    }

    // 数据初始化
    @Override
    public void init(GpProductionDetails gpProductionDetails) {
        GpProductionDetails beforeProductionDetails = this.getById(gpProductionDetails.getId());

        // 判断是否需要新增染厂基础数据
        if (StringUtils.isNotBlank(gpProductionDetails.getDyeingFactoryName())) {
            GpDyeingFactory gpDyeingFactory = iGpDyeingFactoryService.getOne(new LambdaQueryWrapper<GpDyeingFactory>()
                    .eq(GpDyeingFactory::getName, gpProductionDetails.getDyeingFactoryName())
                    .eq(GpDyeingFactory::getDelStatus, YesNoEnum.NO.getKey())
                    .last("limit 1"));
            if (gpDyeingFactory == null) {
                gpDyeingFactory = new GpDyeingFactory();
                gpDyeingFactory.setName(gpProductionDetails.getDyeingFactoryName());
                // 编号获取名字的首字母
                gpDyeingFactory.setCode(StringUtils.getPinYinHeadChar(gpProductionDetails.getDyeingFactoryName()));
                iGpDyeingFactoryService.save(gpDyeingFactory);
            }
            gpProductionDetails.setDyeingFactoryId(gpDyeingFactory.getId());
        }
        if (StringUtils.isBlank(beforeProductionDetails.getSn())) {
            // 如果订单号为空，则生成订单号
            GpDyeingFactory dyeingFactory = iGpDyeingFactoryService.getById(gpProductionDetails.getDyeingFactoryId());
            if (dyeingFactory != null ) {
                gpProductionDetails.setSn(this.getSn(dyeingFactory.getCode()));
            }
        }

        // 判断是否需要新增胚布基础表数据
        if (StringUtils.isNotBlank(gpProductionDetails.getProductName()) && StringUtils.isNotBlank(gpProductionDetails.getSupplierName()) && StringUtils.isNotBlank(gpProductionDetails.getSupplierGreyClothName())) {
            GpGreyCloth gpGreyCloth = iGpGreyClothService.getOne(new LambdaQueryWrapper<GpGreyCloth>()
                    .eq(GpGreyCloth::getName, gpProductionDetails.getProductName())
                    .eq(GpGreyCloth::getSupplier, gpProductionDetails.getSupplierName())
                    .eq(GpGreyCloth::getSupplierGreyClothName, gpProductionDetails.getSupplierGreyClothName())
                    .eq(GpGreyCloth::getDelStatus, YesNoEnum.NO.getKey())
                    .last("limit 1"));
            if (gpGreyCloth == null) {
                gpGreyCloth = new GpGreyCloth();
                gpGreyCloth.setName(gpProductionDetails.getProductName());
                gpGreyCloth.setSupplier(gpProductionDetails.getSupplierName());
                gpGreyCloth.setSupplierGreyClothName(gpProductionDetails.getSupplierGreyClothName());
                iGpGreyClothService.save(gpGreyCloth);
            }
            gpProductionDetails.setSupplierGreyClothId(gpGreyCloth.getId());
        }
        // 判断是否需要新增产品基础表数据
        if (StringUtils.isNotBlank(gpProductionDetails.getProductName()) && StringUtils.isNotBlank(gpProductionDetails.getColour())) {
            GpProduceProduct gpProduceProduct = iGpProduceProductService.getOne(new LambdaQueryWrapper<GpProduceProduct>()
                    .eq(GpProduceProduct::getProductName, gpProductionDetails.getProductName())
                    .eq(GpProduceProduct::getColour, gpProductionDetails.getColour())
                    .eq(GpProduceProduct::getDelStatus, YesNoEnum.NO.getKey())
                    .last("limit 1"));
            if (gpProduceProduct == null) {
                gpProduceProduct = new GpProduceProduct();
                gpProduceProduct.setProductName(gpProductionDetails.getProductName());
                gpProduceProduct.setColour(gpProductionDetails.getColour());
                iGpProduceProductService.save(gpProduceProduct);
            }
            gpProductionDetails.setProduceProductId(gpProduceProduct.getId());
        }
        // 判断是否需要新增颜色对照基础表数据
        if (StringUtils.isNotBlank(gpProductionDetails.getProductName()) && StringUtils.isNotBlank(gpProductionDetails.getColour()) && StringUtils.isNotBlank(gpProductionDetails.getFactoryColourCode())) {
            GpColourComparison gpColourComparison = iGpColourComparisonService.getOne(new LambdaQueryWrapper<GpColourComparison>()
                    .eq(GpColourComparison::getProductName, gpProductionDetails.getProductName())
                    .eq(GpColourComparison::getColour, gpProductionDetails.getColour())
                    .eq(GpColourComparison::getDyeingFactoryColour, gpProductionDetails.getFactoryColourCode())
                    .eq(GpColourComparison::getDelStatus, YesNoEnum.NO.getKey())
                    .last("limit 1"));
            if (gpColourComparison == null) {
                gpColourComparison = new GpColourComparison();
                gpColourComparison.setProductName(gpProductionDetails.getProductName());
                gpColourComparison.setColour(gpProductionDetails.getColour());
                gpColourComparison.setDyeingFactoryColour(gpProductionDetails.getFactoryColourCode());
                gpColourComparison.setDyeingFactoryName(gpProductionDetails.getDyeingFactoryName());
                iGpColourComparisonService.save(gpColourComparison);
            }
            gpProductionDetails.setColourComparisonId(gpColourComparison.getId());
        }
        // 判断是否需要新增颜色备注基础表数据
        if (StringUtils.isNotBlank(gpProductionDetails.getColour()) && StringUtils.isNotBlank(gpProductionDetails.getColourRemarks())) {
            GpColourRemarks gpColourRemarks = iGpColourRemarksService.getOne(new LambdaQueryWrapper<GpColourRemarks>()
                    .eq(GpColourRemarks::getColour, gpProductionDetails.getColour())
                    .eq(GpColourRemarks::getRemarks, gpProductionDetails.getColourRemarks())
                    .eq(GpColourRemarks::getDelStatus, YesNoEnum.NO.getKey())
                    .last("limit 1"));
            if (gpColourRemarks == null) {
                gpColourRemarks = new GpColourRemarks();
                gpColourRemarks.setColour(gpProductionDetails.getColour());
                gpColourRemarks.setRemarks(gpProductionDetails.getColourRemarks());
                iGpColourRemarksService.save(gpColourRemarks);
            }
            gpProductionDetails.setColourRemarksId(gpColourRemarks.getId());
        }
        // 判断是否需要新增做货要求明细基础表数据
        if (StringUtils.isNotBlank(gpProductionDetails.getDyeingFactoryName())
                && StringUtils.isNotBlank(gpProductionDetails.getProductName())
                && StringUtils.isNotBlank(gpProductionDetails.getColour())
                && StringUtils.isNotBlank(gpProductionDetails.getFactoryColourCode())
                && StringUtils.isNotBlank(gpProductionDetails.getFeel())
                && StringUtils.isNotBlank(gpProductionDetails.getLowerMachineWidth())) {
            GpMakingGoodsRequirementDetails gpMakingGoodsRequirementDetails = iGpMakingGoodsRequirementDetailsService.getOne(new LambdaQueryWrapper<GpMakingGoodsRequirementDetails>()
                    .eq(GpMakingGoodsRequirementDetails::getDyeingFactoryName, gpProductionDetails.getDyeingFactoryName())
                    .eq(GpMakingGoodsRequirementDetails::getProductName, gpProductionDetails.getProductName())
                    .eq(GpMakingGoodsRequirementDetails::getColour, gpProductionDetails.getColour())
                    .eq(GpMakingGoodsRequirementDetails::getDyeingFactoryColour, gpProductionDetails.getFactoryColourCode())
                    .eq(GpMakingGoodsRequirementDetails::getFeel, gpProductionDetails.getFeel())
                    .eq(GpMakingGoodsRequirementDetails::getLowerMachineWidth, gpProductionDetails.getLowerMachineWidth())
                    .eq(GpMakingGoodsRequirementDetails::getDelStatus, YesNoEnum.NO.getKey())
                    .last("limit 1"));
            if (gpMakingGoodsRequirementDetails == null) {
                gpMakingGoodsRequirementDetails = new GpMakingGoodsRequirementDetails();
                gpMakingGoodsRequirementDetails.setDyeingFactoryName(gpProductionDetails.getDyeingFactoryName());
                gpMakingGoodsRequirementDetails.setProductName(gpProductionDetails.getProductName());
                gpMakingGoodsRequirementDetails.setColour(gpProductionDetails.getColour());
                gpMakingGoodsRequirementDetails.setDyeingFactoryColour(gpProductionDetails.getFactoryColourCode());
                gpMakingGoodsRequirementDetails.setFeel(gpProductionDetails.getFeel());
                gpMakingGoodsRequirementDetails.setLowerMachineWidth(gpProductionDetails.getLowerMachineWidth());
                iGpMakingGoodsRequirementDetailsService.save(gpMakingGoodsRequirementDetails);
            }
            gpProductionDetails.setMakingGoodsRequirementDetailsId(gpMakingGoodsRequirementDetails.getId());
        }
        // 判断是否需要新增打码规格基础表数据
        if (StringUtils.isNotBlank(gpProductionDetails.getProductName()) && StringUtils.isNotBlank(gpProductionDetails.getCodingSpecifications()) && StringUtils.isNotBlank(gpProductionDetails.getTagSpecifications())) {
            GpCodingSpecifications gpCodingSpecifications = iGpCodingSpecificationsService.getOne(new LambdaQueryWrapper<GpCodingSpecifications>()
                    .eq(GpCodingSpecifications::getProductName, gpProductionDetails.getProductName())
                    .eq(GpCodingSpecifications::getCodingSpecifications, gpProductionDetails.getCodingSpecifications())
                    .eq(GpCodingSpecifications::getTagSpecifications, gpProductionDetails.getTagSpecifications())
                    .eq(GpCodingSpecifications::getDelStatus, YesNoEnum.NO.getKey())
                    .last("limit 1"));
            if (gpCodingSpecifications == null) {
                gpCodingSpecifications = new GpCodingSpecifications();
                gpCodingSpecifications.setProductName(gpProductionDetails.getProductName());
                gpCodingSpecifications.setCodingSpecifications(gpProductionDetails.getCodingSpecifications());
                gpCodingSpecifications.setTagSpecifications(gpProductionDetails.getTagSpecifications());
                iGpCodingSpecificationsService.save(gpCodingSpecifications);
            }
            gpProductionDetails.setCodingSpecificationsId(gpCodingSpecifications.getId());
        }
        // 判断是否需要新增送货地基础表数据
        if (StringUtils.isNotBlank(gpProductionDetails.getDeliveryLocation())) {
            GpDeliveryLocation gpDeliveryLocation = iGpDeliveryLocationService.getOne(new LambdaQueryWrapper<GpDeliveryLocation>()
                    .eq(GpDeliveryLocation::getDeliveryLocation, gpProductionDetails.getDeliveryLocation())
                    .eq(GpDeliveryLocation::getDelStatus, YesNoEnum.NO.getKey())
                    .last("limit 1"));
            if (gpDeliveryLocation == null) {
                gpDeliveryLocation = new GpDeliveryLocation();
                gpDeliveryLocation.setDeliveryLocation(gpProductionDetails.getDeliveryLocation());
                iGpDeliveryLocationService.save(gpDeliveryLocation);
            }
            gpProductionDetails.setDeliveryLocationId(gpDeliveryLocation.getId());
        }
        // 判断是否需要新增物流基础表数据
        if (StringUtils.isNotBlank(gpProductionDetails.getLogistics())) {
            GpLogistics gpLogistics = iGpLogisticsService.getOne(new LambdaQueryWrapper<GpLogistics>()
                    .eq(GpLogistics::getLogistics, gpProductionDetails.getLogistics())
                    .eq(GpLogistics::getDelStatus, YesNoEnum.NO.getKey())
                    .last("limit 1"));
            if (gpLogistics == null) {
                gpLogistics = new GpLogistics();
                gpLogistics.setLogistics(gpProductionDetails.getLogistics());
                iGpLogisticsService.save(gpLogistics);
            }
            gpProductionDetails.setLogisticsId(gpLogistics.getId());
        }

        // 判断是否需要新增备注表基础表数据
        if (StringUtils.isNotBlank(gpProductionDetails.getRemarks())) {
            GpRemarks gpRemarks = iGpRemarksService.getOne(new LambdaQueryWrapper<GpRemarks>()
                    .eq(GpRemarks::getRemarks, gpProductionDetails.getRemarks())
                    .eq(GpRemarks::getDelStatus, YesNoEnum.NO.getKey())
                    .last("limit 1"));

            if (gpRemarks == null) {
                gpRemarks = new GpRemarks();
                gpRemarks.setRemarks(gpProductionDetails.getRemarks());
                gpRemarks.setName(gpProductionDetails.getProductName());
                iGpRemarksService.save(gpRemarks);
            }
            gpProductionDetails.setRemarksId(gpRemarks.getId());
        }


    }

    /**
     * 获取做货明细订单号
     * @param code
     * @return
     */
    private String getSn(String code) {
        String sn = "";
        if (StringUtils.isNotBlank(code)) {
            sn += code + "-";
        }
        String yyMMdd = DateUtils.getDate("yyMMdd");
        sn += yyMMdd;
        GpProductionDetails productionDetails = this.getOne(new LambdaQueryWrapper<GpProductionDetails>()
                .eq(GpProductionDetails::getDelStatus, YesNoEnum.NO.getKey())
                .isNotNull(GpProductionDetails::getSn)
                .gt(GpProductionDetails::getCreateTime, DateUtils.getDate("yyyy-MM-dd 00:00:00"))
                .orderByDesc(GpProductionDetails::getCreateTime)
                .last("limit 1"));
        if (productionDetails == null) {
            sn += "-1";
        } else {
            String substring = productionDetails.getSn().substring(productionDetails.getSn().lastIndexOf("-") + 1);
            int index = Integer.parseInt(substring) + 1;
            sn += "-" + index;
        }
        return sn;
    }




    @Override
    public void initNoSn(GpProductionDetails gpProductionDetails) {
        GpProductionDetails beforeProductionDetails = this.getById(gpProductionDetails.getId());

        // 判断是否需要新增染厂基础数据
        if (StringUtils.isNotBlank(gpProductionDetails.getDyeingFactoryName())) {
            GpDyeingFactory gpDyeingFactory = iGpDyeingFactoryService.getOne(new LambdaQueryWrapper<GpDyeingFactory>()
                    .eq(GpDyeingFactory::getName, gpProductionDetails.getDyeingFactoryName())
                    .eq(GpDyeingFactory::getDelStatus, YesNoEnum.NO.getKey())
                    .last("limit 1"));
            if (gpDyeingFactory == null) {
                gpDyeingFactory = new GpDyeingFactory();
                gpDyeingFactory.setName(gpProductionDetails.getDyeingFactoryName());
                // 编号获取名字的首字母
                gpDyeingFactory.setCode(StringUtils.getPinYinHeadChar(gpProductionDetails.getDyeingFactoryName()));
                iGpDyeingFactoryService.save(gpDyeingFactory);
            }
            gpProductionDetails.setDyeingFactoryId(gpDyeingFactory.getId());
        }

        // 判断是否需要新增胚布基础表数据
        if (StringUtils.isNotBlank(gpProductionDetails.getProductName()) && StringUtils.isNotBlank(gpProductionDetails.getSupplierName()) && StringUtils.isNotBlank(gpProductionDetails.getSupplierGreyClothName())) {
            GpGreyCloth gpGreyCloth = iGpGreyClothService.getOne(new LambdaQueryWrapper<GpGreyCloth>()
                    .eq(GpGreyCloth::getName, gpProductionDetails.getProductName())
                    .eq(GpGreyCloth::getSupplier, gpProductionDetails.getSupplierName())
                    .eq(GpGreyCloth::getSupplierGreyClothName, gpProductionDetails.getSupplierGreyClothName())
                    .eq(GpGreyCloth::getDelStatus, YesNoEnum.NO.getKey())
                    .last("limit 1"));
            if (gpGreyCloth == null) {
                gpGreyCloth = new GpGreyCloth();
                gpGreyCloth.setName(gpProductionDetails.getProductName());
                gpGreyCloth.setSupplier(gpProductionDetails.getSupplierName());
                gpGreyCloth.setSupplierGreyClothName(gpProductionDetails.getSupplierGreyClothName());
                iGpGreyClothService.save(gpGreyCloth);
            }
            gpProductionDetails.setSupplierGreyClothId(gpGreyCloth.getId());
        }
        // 判断是否需要新增产品基础表数据
        if (StringUtils.isNotBlank(gpProductionDetails.getProductName()) && StringUtils.isNotBlank(gpProductionDetails.getColour())) {
            GpProduceProduct gpProduceProduct = iGpProduceProductService.getOne(new LambdaQueryWrapper<GpProduceProduct>()
                    .eq(GpProduceProduct::getProductName, gpProductionDetails.getProductName())
                    .eq(GpProduceProduct::getColour, gpProductionDetails.getColour())
                    .eq(GpProduceProduct::getDelStatus, YesNoEnum.NO.getKey())
                    .last("limit 1"));
            if (gpProduceProduct == null) {
                gpProduceProduct = new GpProduceProduct();
                gpProduceProduct.setProductName(gpProductionDetails.getProductName());
                gpProduceProduct.setColour(gpProductionDetails.getColour());
                iGpProduceProductService.save(gpProduceProduct);
            }
            gpProductionDetails.setProduceProductId(gpProduceProduct.getId());
        }
        // 判断是否需要新增颜色对照基础表数据
        if (StringUtils.isNotBlank(gpProductionDetails.getProductName()) && StringUtils.isNotBlank(gpProductionDetails.getColour()) && StringUtils.isNotBlank(gpProductionDetails.getFactoryColourCode())) {
            GpColourComparison gpColourComparison = iGpColourComparisonService.getOne(new LambdaQueryWrapper<GpColourComparison>()
                    .eq(GpColourComparison::getProductName, gpProductionDetails.getProductName())
                    .eq(GpColourComparison::getColour, gpProductionDetails.getColour())
                    .eq(GpColourComparison::getDyeingFactoryColour, gpProductionDetails.getFactoryColourCode())
                    .eq(GpColourComparison::getDelStatus, YesNoEnum.NO.getKey())
                    .last("limit 1"));
            if (gpColourComparison == null) {
                gpColourComparison = new GpColourComparison();
                gpColourComparison.setProductName(gpProductionDetails.getProductName());
                gpColourComparison.setColour(gpProductionDetails.getColour());
                gpColourComparison.setDyeingFactoryColour(gpProductionDetails.getFactoryColourCode());
                gpColourComparison.setDyeingFactoryName(gpProductionDetails.getDyeingFactoryName());
                iGpColourComparisonService.save(gpColourComparison);
            }
            gpProductionDetails.setColourComparisonId(gpColourComparison.getId());
        }
        // 判断是否需要新增颜色备注基础表数据
        if (StringUtils.isNotBlank(gpProductionDetails.getColour()) && StringUtils.isNotBlank(gpProductionDetails.getColourRemarks())) {
            GpColourRemarks gpColourRemarks = iGpColourRemarksService.getOne(new LambdaQueryWrapper<GpColourRemarks>()
                    .eq(GpColourRemarks::getColour, gpProductionDetails.getColour())
                    .eq(GpColourRemarks::getRemarks, gpProductionDetails.getColourRemarks())
                    .eq(GpColourRemarks::getDelStatus, YesNoEnum.NO.getKey())
                    .last("limit 1"));
            if (gpColourRemarks == null) {
                gpColourRemarks = new GpColourRemarks();
                gpColourRemarks.setColour(gpProductionDetails.getColour());
                gpColourRemarks.setRemarks(gpProductionDetails.getColourRemarks());
                iGpColourRemarksService.save(gpColourRemarks);
            }
            gpProductionDetails.setColourRemarksId(gpColourRemarks.getId());
        }
        // 判断是否需要新增做货要求明细基础表数据
        if (StringUtils.isNotBlank(gpProductionDetails.getDyeingFactoryName())
                && StringUtils.isNotBlank(gpProductionDetails.getProductName())
                && StringUtils.isNotBlank(gpProductionDetails.getColour())
                && StringUtils.isNotBlank(gpProductionDetails.getFactoryColourCode())
                && StringUtils.isNotBlank(gpProductionDetails.getFeel())
                && StringUtils.isNotBlank(gpProductionDetails.getLowerMachineWidth())) {
            GpMakingGoodsRequirementDetails gpMakingGoodsRequirementDetails = iGpMakingGoodsRequirementDetailsService.getOne(new LambdaQueryWrapper<GpMakingGoodsRequirementDetails>()
                    .eq(GpMakingGoodsRequirementDetails::getDyeingFactoryName, gpProductionDetails.getDyeingFactoryName())
                    .eq(GpMakingGoodsRequirementDetails::getProductName, gpProductionDetails.getProductName())
                    .eq(GpMakingGoodsRequirementDetails::getColour, gpProductionDetails.getColour())
                    .eq(GpMakingGoodsRequirementDetails::getDyeingFactoryColour, gpProductionDetails.getFactoryColourCode())
                    .eq(GpMakingGoodsRequirementDetails::getFeel, gpProductionDetails.getFeel())
                    .eq(GpMakingGoodsRequirementDetails::getLowerMachineWidth, gpProductionDetails.getLowerMachineWidth())
                    .eq(GpMakingGoodsRequirementDetails::getDelStatus, YesNoEnum.NO.getKey())
                    .last("limit 1"));
            if (gpMakingGoodsRequirementDetails == null) {
                gpMakingGoodsRequirementDetails = new GpMakingGoodsRequirementDetails();
                gpMakingGoodsRequirementDetails.setDyeingFactoryName(gpProductionDetails.getDyeingFactoryName());
                gpMakingGoodsRequirementDetails.setProductName(gpProductionDetails.getProductName());
                gpMakingGoodsRequirementDetails.setColour(gpProductionDetails.getColour());
                gpMakingGoodsRequirementDetails.setDyeingFactoryColour(gpProductionDetails.getFactoryColourCode());
                gpMakingGoodsRequirementDetails.setFeel(gpProductionDetails.getFeel());
                gpMakingGoodsRequirementDetails.setLowerMachineWidth(gpProductionDetails.getLowerMachineWidth());
                iGpMakingGoodsRequirementDetailsService.save(gpMakingGoodsRequirementDetails);
            }
            gpProductionDetails.setMakingGoodsRequirementDetailsId(gpMakingGoodsRequirementDetails.getId());
        }
        // 判断是否需要新增打码规格基础表数据
        if (StringUtils.isNotBlank(gpProductionDetails.getProductName()) && StringUtils.isNotBlank(gpProductionDetails.getCodingSpecifications()) && StringUtils.isNotBlank(gpProductionDetails.getTagSpecifications())) {
            GpCodingSpecifications gpCodingSpecifications = iGpCodingSpecificationsService.getOne(new LambdaQueryWrapper<GpCodingSpecifications>()
                    .eq(GpCodingSpecifications::getProductName, gpProductionDetails.getProductName())
                    .eq(GpCodingSpecifications::getCodingSpecifications, gpProductionDetails.getCodingSpecifications())
                    .eq(GpCodingSpecifications::getTagSpecifications, gpProductionDetails.getTagSpecifications())
                    .eq(GpCodingSpecifications::getDelStatus, YesNoEnum.NO.getKey())
                    .last("limit 1"));
            if (gpCodingSpecifications == null) {
                gpCodingSpecifications = new GpCodingSpecifications();
                gpCodingSpecifications.setProductName(gpProductionDetails.getProductName());
                gpCodingSpecifications.setCodingSpecifications(gpProductionDetails.getCodingSpecifications());
                gpCodingSpecifications.setTagSpecifications(gpProductionDetails.getTagSpecifications());
                iGpCodingSpecificationsService.save(gpCodingSpecifications);
            }
            gpProductionDetails.setCodingSpecificationsId(gpCodingSpecifications.getId());
        }
        // 判断是否需要新增送货地基础表数据
        if (StringUtils.isNotBlank(gpProductionDetails.getDeliveryLocation())) {
            GpDeliveryLocation gpDeliveryLocation = iGpDeliveryLocationService.getOne(new LambdaQueryWrapper<GpDeliveryLocation>()
                    .eq(GpDeliveryLocation::getDeliveryLocation, gpProductionDetails.getDeliveryLocation())
                    .eq(GpDeliveryLocation::getDelStatus, YesNoEnum.NO.getKey())
                    .last("limit 1"));
            if (gpDeliveryLocation == null) {
                gpDeliveryLocation = new GpDeliveryLocation();
                gpDeliveryLocation.setDeliveryLocation(gpProductionDetails.getDeliveryLocation());
                iGpDeliveryLocationService.save(gpDeliveryLocation);
            }
            gpProductionDetails.setDeliveryLocationId(gpDeliveryLocation.getId());
        }
        // 判断是否需要新增物流基础表数据
        if (StringUtils.isNotBlank(gpProductionDetails.getLogistics())) {
            GpLogistics gpLogistics = iGpLogisticsService.getOne(new LambdaQueryWrapper<GpLogistics>()
                    .eq(GpLogistics::getLogistics, gpProductionDetails.getLogistics())
                    .eq(GpLogistics::getDelStatus, YesNoEnum.NO.getKey())
                    .last("limit 1"));
            if (gpLogistics == null) {
                gpLogistics = new GpLogistics();
                gpLogistics.setLogistics(gpProductionDetails.getLogistics());
                iGpLogisticsService.save(gpLogistics);
            }
            gpProductionDetails.setLogisticsId(gpLogistics.getId());
        }

        // 判断是否需要新增备注表基础表数据
        if (StringUtils.isNotBlank(gpProductionDetails.getRemarks())) {
            GpRemarks gpRemarks = iGpRemarksService.getOne(new LambdaQueryWrapper<GpRemarks>()
                    .eq(GpRemarks::getRemarks, gpProductionDetails.getRemarks())
                    .eq(GpRemarks::getDelStatus, YesNoEnum.NO.getKey())
                    .last("limit 1"));

            if (gpRemarks == null) {
                gpRemarks = new GpRemarks();
                gpRemarks.setRemarks(gpProductionDetails.getRemarks());
                gpRemarks.setName(gpProductionDetails.getProductName());
                iGpRemarksService.save(gpRemarks);
            }
            gpProductionDetails.setRemarksId(gpRemarks.getId());
        }


    }
}
